﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Class1 的摘要说明
/// </summary>
public class Class1
{
	public Class1()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}
    public static SqlConnection DBconnection()
    {
        return new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

    }
    public static DataTable ExecSel(string sql)
    {
        SqlConnection con = Class1.DBconnection();
        con.Open();
        SqlCommand com = new SqlCommand(sql, con);
        SqlDataAdapter oda = new SqlDataAdapter();
        DataSet ds = new DataSet();
        oda.SelectCommand = com;
        oda.Fill(ds);
        ds.Dispose();
        con.Close();
        con.Dispose();
        com.Dispose();
        return ds.Tables[0];

    }
    public static string SelOne(string sql)
    {
        SqlConnection con = Class1.DBconnection();
        con.Open();
        SqlCommand com = new SqlCommand(sql, con);
        return com.ExecuteScalar().ToString();
        con.Close();
        con.Dispose();
        com.Dispose();
    }
    public static void ExecSql(string sql)
    {
        SqlConnection con = Class1.DBconnection();
        con.Open();
        SqlCommand com = new SqlCommand(sql, con);
        com.ExecuteNonQuery();
        con.Close();
        con.Dispose();
        com.Dispose();
       
    }
    public static IDataReader GetDataReader(string sql)
    {
        SqlConnection con = Class1.DBconnection();
        con.Open();
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader da = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        return da;
        con.Close();
        con.Dispose();
        cmd.Dispose();
    }
    public static string GetSqlResult(string sql)
    {
        string str = "";
        SqlConnection con = Class1.DBconnection();
        con.Open();
        SqlCommand cmd = new SqlCommand(sql, con);
        if (cmd.ExecuteScalar() == null)
        {
            con.Close();
            return "";
        }
        else
        {
            str = cmd.ExecuteScalar().ToString();
        }
        con.Close();
        con.Dispose();
        cmd.Dispose();
        return str;

    }
    public static int ExecCommand(string sql)
    {
        SqlConnection con = Class1.DBconnection();
        con.Open();
        SqlCommand cmd = new SqlCommand(sql, con);
        int i = cmd.ExecuteNonQuery();
        cmd.Dispose();
        con.Close();
        con.Dispose();
        return i;

    }
    public static void AlertShow(string js)
    {
        HttpContext.Current.Response.Write("<script>alert('" + js + "');history.go(-1);</script>");
    }
    public static void AlertShow(string js, string url)
    {
        HttpContext.Current.Response.Write("<script>alert('" + js + "');location='" + url + "'</script>");
    }
}
